package com.doublefish.doublefishrobot.ui.home.adapter;

import android.content.Context;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.home.bean.DeviceBean;
import com.doublefish.doublefishrobot.utils.ble.bean.ModeConstant;

import java.util.List;

/**
 * 作者:dugaofeng on 2025/4/15 11:28
 */
public class AddDeviceListAdapter extends BaseMultiItemQuickAdapter<AddDeviceListAdapter.BleDeviceEntity, BaseViewHolder> {

    Context context;
    public AddDeviceListAdapter(Context context, List<BleDeviceEntity> data) {
        super(data);
        this.context = context;
        addItemType(BleDeviceEntity.HEADER, R.layout.item_bind_device_header);
        addItemType(BleDeviceEntity.CELL, R.layout.item_bind_device2);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder
                                   helper, BleDeviceEntity entity) {
        // 根据返回的 type 分别设置数据
        switch (helper.getItemViewType()) {
            case BleDeviceEntity.HEADER: {
                helper.setTextColor(R.id.tv_text, context.getColor(entity.isBind ? R.color.black : R.color.red));
                helper.setText(R.id.tv_text, entity.getTitle());
            }
            break;

            case BleDeviceEntity.CELL: {
                if (entity.getDeviceBean() == null) {
                    helper.setText(R.id.tv_name, context.getString(R.string.search_null_device));
                    helper.setText(R.id.tv_device_type, context.getString(R.string.click_refresh));
                    helper.setImageResource(R.id.iv_icon, R.mipmap.icon_default);
                    helper.setVisible(R.id.tv_state, false);
                } else {
                    helper.setText(R.id.tv_name, entity.getDeviceBean().getAlias());
                    helper.setText(R.id.tv_device_type, entity.getDeviceBean().getDeviceTypeName());
                    int resId = R.mipmap.icon_default;
                    if (entity.getDeviceBean().getDeviceType() == ModeConstant.DeviceTypeS8) {
                        resId = R.mipmap.super8_2;
                    } else if (entity.getDeviceBean().getDeviceType() == ModeConstant.DeviceTypeS6) {
                        resId = R.mipmap.super6_2;
                    } else if (entity.getDeviceBean().getDeviceType() == ModeConstant.DeviceTypeS5) {
                        resId = R.mipmap.super_5;
                    } else if (entity.getDeviceBean().getDeviceType() == ModeConstant.DeviceTypeS3) {
                        resId = R.mipmap.super_3;
                    }
                    helper.setImageResource(R.id.iv_icon, resId);
                    helper.setVisible(R.id.tv_state, entity.isBind());
                }
            }
            break;

            default:
                break;
        }
    }

    public static class BleDeviceEntity implements MultiItemEntity {

        public static final int HEADER = 0;
        public static final int CELL = 1;

        private boolean isHeader;  //是否是标题

        private String title;
        private DeviceBean deviceBean;
        private boolean isBind;

        public BleDeviceEntity(boolean isHeader) {
            this.isHeader = isHeader;
        }

        public boolean isHeader() {
            return isHeader;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public DeviceBean getDeviceBean() {
            return deviceBean;
        }

        public void setDeviceBean(DeviceBean deviceBean) {
            this.deviceBean = deviceBean;
        }

        public boolean isBind() {
            return isBind;
        }

        public void setBind(boolean bind) {
            isBind = bind;
        }

        @Override
        public int getItemType() {
            return isHeader ? HEADER : CELL;
        }
    }
}